package com.mf.morefun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.mf.morefun.MoreFunApplication;
import com.mf.morefun.R;
import com.mf.morefun.bean.PMUserBean;
import com.mf.morefun.listener.ClassNumListen;
import com.mf.morefun.listener.MyRequestListener;
import com.mf.morefun.ui.moreread.TestActivity;
import com.mf.morefun.util.IsLoginBean;
import com.mf.morefun.util.TransformationUtils;
import com.zhcx.zhcxlibrary.utils.DateUtils;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PMUserAdapter extends BaseAdapter {
    private Context mContext;
    private List<PMUserBean> mPMUserList;
    private ClassNumListen listen;

    public ClassNumListen getListen() {
        return listen;
    }

    public void setListen(ClassNumListen listen) {
        this.listen = listen;
    }

    public PMUserAdapter(Context mContext, List<PMUserBean> mPMUserList) {
        this.mContext = mContext;
        this.mPMUserList = mPMUserList;
    }

    @Override
    public int getCount() {
        return mPMUserList.size();
    }

    @Override
    public Object getItem(int position) {
        return mPMUserList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_classnumitem, null);
        } else {
            view = convertView;
        }
        if (null != mPMUserList && mPMUserList.size() > 0) {
            ViewHolder holder = new ViewHolder(view);
            if ((position % 2) == 0) {
                holder.llBg.setBackgroundResource(R.color.nomalbg_color);
            } else {
                holder.llBg.setBackgroundResource(R.color.white);
            }
            holder.tvName.setText(mPMUserList.get(position).getName());
            holder.tvPhone.setText(mPMUserList.get(position).getPhone());
            //设置图片圆角角度
            CircleCrop circleCrop = new CircleCrop();
            RequestOptions options = RequestOptions.bitmapTransform(circleCrop);
            Glide.with(mContext).asBitmap().load(mPMUserList.get(position).getHeadPortrait()).listener(new MyRequestListener(holder.ivHead, mContext, DeviceUtils.dip2px(mContext, 28), DeviceUtils.dip2px(mContext, 28))).apply(options).into(new TransformationUtils(holder.ivHead, mContext, DeviceUtils.dip2px(mContext, 28), DeviceUtils.dip2px(mContext, 28)));
            holder.tvType.setText("已过期");//是否过期  通过有效期来算
            try {
                if (StringUtils.isEmpty(mPMUserList.get(position).getEnableEnd())) {
                    holder.tvTime.setText("no time");
                    holder.tvType.setText("异常");
                } else {
                    if (mPMUserList.get(position).getEnableEnd().length() == 10) {
                        long enableTime = DateUtils.DEFAULT_DATE_FORMAT.parse(mPMUserList.get(position).getEnableEnd() + " 23:59:59").getTime();
                        long currentTime = System.currentTimeMillis();
                        if (enableTime >= currentTime) {
                            holder.tvType.setText("正常");
                            holder.tvType.setTextColor(0xff666666);
                            holder.tvType.setTextColor(mContext.getResources().getColor(R.color.num_color));
                        } else {
                            holder.tvType.setText("已过期");
                            holder.tvType.setTextColor(mContext.getResources().getColor(R.color.num_color));
                        }
                        holder.tvTime.setText(mPMUserList.get(position).getEnableEnd());
                    } else {
                        long enableTime = DateUtils.DEFAULT_DATE_FORMAT.parse(mPMUserList.get(position).getEnableEnd()).getTime();
                        long currentTime = System.currentTimeMillis();
                        if (enableTime >= currentTime) {
                            holder.tvType.setText("正常");
                            holder.tvType.setTextColor(0xff666666);
                        } else {
                            holder.tvType.setText("已过期");
                            holder.tvType.setTextColor(mContext.getResources().getColor(R.color.num_color));
                        }
                        holder.tvTime.setText(DateUtils.DATE_YEAR_MONTH_DAY.format(new Date(enableTime)));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                holder.tvTime.setText("no time");
                holder.tvType.setText("异常");
            }
            if (StringUtils.isEmpty(mPMUserList.get(position).getUserType())) {
                holder.tvUsertype.setText("未知");
            } else {
                if (mPMUserList.get(position).getUserType().equals("1")) {
                    holder.tvUsertype.setText("学员");
                } else if (mPMUserList.get(position).getUserType().equals("2")) {//2老师
                    holder.tvUsertype.setText("老师");
                } else if (mPMUserList.get(position).getUserType().equals("3")) {//3管理员
                    holder.tvUsertype.setText("管理员");
                }
            }
            if (IsLoginBean.isNo(true)) {
                if (StringUtils.isEmpty(MoreFunApplication.loginBean.getResult().getUserType())) {
                    holder.tvDo.setVisibility(View.GONE);
                } else {
                    if (MoreFunApplication.loginBean.getResult().getUserType().equals("1")) {
                        holder.tvDo.setVisibility(View.GONE);
                    } else if (MoreFunApplication.loginBean.getResult().getUserType().equals("2")) {//2老师
                        holder.tvDo.setVisibility(View.GONE);
                    } else if (MoreFunApplication.loginBean.getResult().getUserType().equals("3")) {//3管理员
                        holder.tvDo.setVisibility(View.VISIBLE);
                    }
                }
            } else {
                holder.tvDo.setVisibility(View.GONE);
            }
            holder.tvDo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listen.callBack(position, holder.tvDo);
                }
            });
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.iv_head)
        ImageView ivHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_phone)
        TextView tvPhone;
        @BindView(R.id.tv_usertype)
        TextView tvUsertype;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_do)
        TextView tvDo;
        @BindView(R.id.ll_bg)
        LinearLayout llBg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
